let express = require("express")();
let mysql = require("mysql");
let expressStatic = require("express-static");
var sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'student',
    timezone: "08:00"

});

sql.connect();
express.get("/getStudents", (request, response) => {
    sql.query(`SELECT * FROM students`, (error, data) => {
        response.send(data)
    })
})
express.get("/deleteStudents", (request, response) => {
    sql.query(`DELETE FROM students WHERE id=${request.query.id}`, (error, data) => {
        if (error) {
            response.send("error")
        } else {
            response.send("success")
        }
    })
})
express.get("/addStudents", (request, response) => {
    let txt = request.query
    sql.query(`INSERT INTO students (name,age,sex,city,jsonData) VALUES ("${txt.name}","${txt.age}","${txt.sex}","${txt.city}","${txt.date}")`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            response.send("success")
        }
    })
})
express.get("/redactStudents", (request, response) => {
    let txt = request.query
    sql.query(`UPDATE students SET name="${txt.name}",age="${txt.age}",sex="${txt.sex}",city="${txt.city}",jsonData="${txt.date}" WHERE id="${txt.id}"`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            response.send("success")
        }
    })
})







express.use(expressStatic(__dirname + '/students'));
express.listen(81)